


#include "LoadGame.h"

#include "../source/Game.h"
#include "../source/BitmapFont.h"

#include "../GameStates/Play.h"

#include "../SGD Wrappers/SGD_InputManager.h"

#include "../TinyXML/tinyxml.h"


LoadGame* LoadGame::GetInstance()
{
	static LoadGame s_pInstance; 
	return &s_pInstance; 
}

void	LoadGame::Enter(void)
{

}

void	LoadGame::Exit(void)
{

}

bool	LoadGame::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance(); 

	if (m_bYourMom == true)
	{
		if (pInput->IsKeyPressed(SGD::Key::Down) || pInput->IsDPadPressed(0, SGD::DPad::Down))
		{
			m_nSavesCursor++; 
			if (m_nSavesCursor > m_nSavesCursorMax)
				m_nSavesCursor = 0;
		}
		else if (pInput->IsKeyPressed(SGD::Key::Up) || pInput->IsDPadPressed(0, SGD::DPad::Up))
		{
			m_nSavesCursor--;
			if (m_nSavesCursor < 0)
				m_nSavesCursor = m_nSavesCursorMax;
		}
	}

	TiXmlDocument doc;

	if (m_nSavesCursor == 0)
	{
		if (doc.LoadFile("resource/data/Slot1Save.xml") == true)
		{
			TiXmlElement* pRoot = doc.RootElement();

			TiXmlElement* pWhatLevel = pRoot->FirstChildElement("CurrentLevel");
			pWhatLevel->Attribute("CurrLevel", &m_nWhatLevel);

			TiXmlElement* pPlayerLives = pRoot->FirstChildElement("CurrentLives");
			pPlayerLives->Attribute("P1CurrLives", &m_nP1Lives);
			pPlayerLives->Attribute("P2CurrLives", &m_nP2Lives);

			TiXmlElement* pPlayerScore = pRoot->FirstChildElement("CurrentScore");
			pPlayerScore->Attribute("P1CurrScore", &m_nP1Score);
			pPlayerScore->Attribute("P2CurrScore", &m_nP2Score);
		}
	}
	else if (m_nSavesCursor == 1)
	{
		if (doc.LoadFile("resource/data/Slot2Save.xml") == true)
		{
			TiXmlElement* pRoot = doc.RootElement();

			TiXmlElement* pWhatLevel = pRoot->FirstChildElement("CurrentLevel");
			pWhatLevel->Attribute("CurrLevel", &m_nWhatLevel);

			TiXmlElement* pPlayerLives = pRoot->FirstChildElement("CurrentLives");
			pPlayerLives->Attribute("P1CurrLives", &m_nP1Lives);
			pPlayerLives->Attribute("P2CurrLives", &m_nP2Lives);

			TiXmlElement* pPlayerScore = pRoot->FirstChildElement("CurrentScore");
			pPlayerScore->Attribute("P1CurrScore", &m_nP1Score);
			pPlayerScore->Attribute("P2CurrScore", &m_nP2Score);
		}
	}
	else if (m_nSavesCursor == 2)
	{
		if (doc.LoadFile("resource/data/Slot3Save.xml") == true)
		{
			TiXmlElement* pRoot = doc.RootElement();

			TiXmlElement* pWhatLevel = pRoot->FirstChildElement("CurrentLevel");
			pWhatLevel->Attribute("CurrLevel", &m_nWhatLevel);

			TiXmlElement* pPlayerLives = pRoot->FirstChildElement("CurrentLives");
			pPlayerLives->Attribute("P1CurrLives", &m_nP1Lives);
			pPlayerLives->Attribute("P2CurrLives", &m_nP2Lives);

			TiXmlElement* pPlayerScore = pRoot->FirstChildElement("CurrentScore");
			pPlayerScore->Attribute("P1CurrScore", &m_nP1Score);
			pPlayerScore->Attribute("P2CurrScore", &m_nP2Score);
		}
	}

	Game::GetInstance()->SetSavesCursor(m_nSavesCursor);
	Game::GetInstance()->SetSavesCursorMax(m_nSavesCursorMax);

	Game::GetInstance()->SetP1Lives(m_nP1Lives);
	Game::GetInstance()->SetP2Lives(m_nP2Lives);

	Game::GetInstance()->SetP1Score(m_nP1Score);
	Game::GetInstance()->SetP2Score(m_nP2Score);

	Game::GetInstance()->SetWhatLevel(m_nWhatLevel);

	if (pInput->IsKeyPressed(SGD::Key::Enter) == true || pInput->IsButtonPressed(0,0))
	{
		Game::GetInstance()->ChangeState(Play::GetInstance());
	}

	return true; 
}

void	LoadGame::Update(float dt)
{

}

void	LoadGame::Render(void)
{
	int width = (int)Game::GetInstance()->GetScreenWidth();
	int height = (int)Game::GetInstance()->GetScreenHeight();
	BitmapFont* pFont = Game::GetInstance()->GetFont();

	pFont->Draw("Which slot would you like to open", width / 2 - 300, height / 2 - 300, 1, SGD::Color{ 255, 200, 0 });

	if (m_nSavesCursor == SLOT_1)
	{
		pFont->Draw("Slot 1", width / 2 - 150, height / 2 - 150, 1, SGD::Color{ 255, 0, 0 });
	}
	else
	{
		pFont->Draw("Slot 1", width / 2 - 150, height / 2 - 150, 1, SGD::Color{ 255, 200, 0 });
	}

	if (m_nSavesCursor == SLOT_2)
	{
		pFont->Draw("Slot 2", width / 2 - 150, height / 2 - 100, 1, SGD::Color{ 255, 0, 0 });
	}
	else
	{
		pFont->Draw("Slot 2", width / 2 - 150, height / 2 - 100, 1, SGD::Color{ 255, 200, 0 });
	}

	if (m_nSavesCursor == SLOT_3)
	{
		pFont->Draw("Slot 3", width / 2 - 150, height / 2 - 50, 1, SGD::Color{ 255, 0, 0 });
	}
	else
	{
		pFont->Draw("Slot 3", width / 2 - 150, height / 2 - 50, 1, SGD::Color{ 255, 200, 0 });
	}

}
